<?php

namespace app\common\model\room\department;

use app\common\enum\DeviceEnum;
use app\common\enum\InitEnum;
use app\common\model\BaseModel;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\Device;
use think\model\concern\SoftDelete;

/**
 * 科室设备关联模型
 * Class RoomDepartmentDevice
 * @package app\common\model\room
 */
class RoomDepartmentDevice extends BaseModel {

    protected $name = 'room_department_device';

//    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $append = ['sort', 'total_fee', 'departments', 'devices_type', 'devices', 'init_text', 'device_type_text'];

    public function building() {
        return $this->hasOne(Building::class, 'id', 'bid');
    }

    public function campus() {
        return $this->hasOne(Campus::class, 'id', 'cid');
    }

    public function room() {
        return $this->hasOne(RoomDepartment::class, 'id', 'rid')->with("building");
    }

    public function device() {
        return $this->hasOne(Device::class, 'id', 'device_id')->append([
            "device_type"
        ]);
    }

    public function deviceItem() {
        return $this->hasOne(Device::class, 'id', 'device_id');
    }

    public function getCopyAddAttr($value, $data) {
        // 禁用append
        return "";
    }

    public function getNoAppendAttr($value, $data) {
        // 禁用append
        return "";
    }

    public function getSortAttr($value, $item) {
        return $this->handle_sort($item['sort']);
    }

    public function getTotalFeeAttr($value, $item) {
        return 1 * sprintf("%.2f", $value);
    }

    public function getInitTextAttr($value, $data) {
        return InitEnum::getDisableDesc($data['is_init']);
    }

    public function getDeviceAttr($value, $data) {
        $result = !empty($value) ? json_decode($value, true) : [];
        $retrun = [];
        foreach ($result as &$v) {
            $retrun[] = (int)$v;
        }
        return $retrun;
    }

    public function getDepartmentAttr($value, $data) {
        $result = !empty($value) ? json_decode($value, true) : [];
        $retrun = [];
        foreach ($result as &$v) {
            $retrun[] = (int)$v;
        }
        return $retrun;
    }

    public function getDevicesAttr($value, $data) {
        $result = '';
        if (!empty($data['device_id'])) {
            $device = Device::where('id', $data['device_id'])->find();
            $campus = Campus::where('id', $device['cid'])->find();
            $result .= $campus['name'] . '-';
            $result .= $device['name'];
        }
        return $result;
    }


    public function getDepartmentsAttr($value, $data) {
        $result = '';
        if (!empty($data['department_id'])) {
            $room_department = RoomDepartment::where('id', $data['department_id'])->find();
            $building = Building::where('id', $room_department['bid'])->with('campus')->find();
            $result .= $building['campus']['name'] . '-';
            $result .= $building['name'] . '-';
            $result .= $room_department['name'];
        }
        return $result;
    }


    public function getDevicesTypeAttr($value, $data) {
        $result = '';
        // if (!empty($data['device_id'])) {
        //     $device = Device::where('id', $data['device_id'])->find();
        //     $result .=  DeviceEnum::getDisableDesc($device['type']);
        // }
        return $result;
    }

    public function getDeviceTypeTextAttr($value, $data) {
        return DeviceEnum::getDisableDesc($data['device_type']);
    }

}
